<?php
/* ******************************
	Incoming Tasks aJax actions
   ******************************
*/
	include('../../includes/classes/core.php');
	$action = $_REQUEST['act'];
	$product=$_REQUEST['product'];
	$client     =   $_REQUEST[id];
	$start		=	$_REQUEST['start'];
    $end		=	$_REQUEST['end'];

	switch ($action) {
	    case 'opt':
	        
	        $query = mysql_query("SELECT DISTINCT `name` FROM `overhead_detail` ORDER BY `name` DESC");
	        
	        while ($temp = mysql_fetch_array($query)) {
	            
	            $data[select][]= "<option>".$temp[0]."</option>";
	        };
	        echo json_encode( $data );
	       break ;
	    case 'single_chart': 
	        $rResult = mysql_query("
	            
SELECT DISTINCT  date(overhead.confirmation_date),
				reserve.real_price,
				overhead.seller_name 
FROM reserve
JOIN overhead 						ON overhead.id=reserve.overhead_id
JOIN production 					ON reserve.production_id=production.id
JOIN production_category 	ON production_category.id=production.category
WHERE production_category.parent_id!=0 AND production_category.`name`='$product' AND overhead.seller_name='$client'
 ORDER BY overhead.id

	             ");
	        
	        
	            while ( $aRow = mysql_fetch_array( $rResult ) )
	            {
	        
	            $data[xAxis][categories][] = $aRow[0];
	            $data[series][0][data][] = (float) $aRow[1];
	            $data[series][0][name] = $aRow[2];
	        
	        
	            }
	        
	            echo json_encode( $data );
	        break;  
	    case 'get_list':
    	    $count = $_REQUEST['count'];

    	    $rResult = mysql_query("SELECT  overhead.id, overhead.seller_name,
                                            round(( SELECT reserve.real_price			 
                                                    FROM reserve
                                                    JOIN overhead 			as ov	ON ov.id=reserve.overhead_id
                                                    JOIN production 					ON reserve.production_id=production.id
                                                    JOIN production_category 	ON production_category.id=production.category
                                                    WHERE production_category.parent_id!=0 AND production_category.`name`='$product' AND overhead.seller_name=ov.seller_name
                                                    ORDER BY overhead.id  DESC LIMIT 1
                                                 ),2)	 
                                    FROM reserve
                                    JOIN overhead 						ON overhead.id=reserve.overhead_id
                                    JOIN production 					ON reserve.production_id=production.id
                                    JOIN production_category 	ON production_category.id=production.category
                                    WHERE production_category.parent_id!=0 AND production_category.`name`='$product'
                                    GROUP BY overhead.seller_name");

			$output = array(
				"aaData"	=> array()
			);

			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
				$row = array();
				for ( $i = 0 ; $i < $count ; $i++ )
				{
					/* General output */
					$row[] = $aRow[$i];
				}
				$output['aaData'][] = $row;
			}

			echo json_encode( $output );

	        break;
	    case 'chart':
	        $data = array();
          
	            
	    	$rResult = mysql_query("SELECT  overhead.id, overhead.seller_name,
                                            round(( SELECT reserve.real_price			 
                                                    FROM reserve
                                                    JOIN overhead 			as ov	ON ov.id=reserve.overhead_id
                                                    JOIN production 					ON reserve.production_id=production.id
                                                    JOIN production_category 	ON production_category.id=production.category
                                                    WHERE production_category.parent_id!=0 AND production_category.`name`='$product' AND overhead.seller_name=ov.seller_name
                                                    ORDER BY overhead.id  DESC LIMIT 1
                                                 ),2)	 
                                    FROM reserve
                                    JOIN overhead 						ON overhead.id=reserve.overhead_id
                                    JOIN production 					ON reserve.production_id=production.id
                                    JOIN production_category 	ON production_category.id=production.category
                                    WHERE production_category.parent_id!=0 AND production_category.`name`='$product'
                                    GROUP BY overhead.seller_name");

	    	
	    	while ( $aRow = mysql_fetch_array( $rResult ) )
	    	{

	    			$data[xAxis][categories][] = $aRow[1];
	    			$data[series][0][data][] = (float) $aRow[2];
	    			$data[series][0][name] = $aRow[1];
	    			

	    	}

	    	echo json_encode( $data );
	    	break;

	    default:
	        $data[error]="action is null";
	       	echo json_encode( $data );
	}
?>